<?php

class Sys_Init
{	
	function LoadBaseInfoToSession()
	{
		$_SESSION['init'] = array();
		
		$mod_params = new Db_Tables_SysParams();
		$_SESSION['init'] = $mod_params->BaseInfo();
		
		//页顶公告
		
		$_SESSION['site']['news'] = '';
		
		if($_SESSION['init']['news_switch'] && $_SESSION['init']['news_contents'])
		{
			$_SESSION['site']['news'] = $_SESSION['init']['news_contents'];
		}
		
		return TRUE;
	}
	
	function GetJsonVerify($postStr)
    {
		$result = array();

		$log_table = new Db_Tables_SysApiLog();

		$str_array = Zend_Json::decode($postStr);

		if(is_array($str_array))
		{
				$result[0] = 1;
				$result[1] = $str_array;

				$log_table->log_contents = $postStr;
		}else{
				$result[0] = 0;
				$result[1] = 1001;

				$log_table->log_contents = Zend_Json::encode($postStr);
		}

		$log_table->log_status = $result[0];
		$log_table->AddRecord();

		return $result;
    }
    
    function IdentityVerify($arr)
    {
        $result = array();
        
        if(is_array($arr))
        {
            if($arr['verify']['userid'] && $arr['verify']['pwd'] && $arr['verify']['token'])
			{
				$token_table = new Db_Tables_SysToken();
				$token_table->userid = $arr['verify']['userid'];
				$token_table->pwd = $arr['verify']['pwd'];
				$token_table->token = $arr['verify']['token'];
				if($token_table->VerifyToken())
				{
					//Passed
					$result[0] = 1;
				}else{
					$result[0] = 0;
					$result[1] = 1004;
				}
				
			}else{
				$result[0] = 0;
				$result[1] = 1003;
			}
        }else{
            $result[0] = 0;
            $result[1] = 1002;
        }
		
		return $result;
    }
	
	function ErrorJsonDecolation($error_code)
	{
		$result = array(
			'general' => array(
				'error_code' => $error_code
			)
		);
		
		return Zend_Json::encode($result);
	}
	
	/**
	 * Return Json
	 */
	function FinalDecolation($port, $feedback_array, $userid, $token)
	{
		//initialize
		$result_array = array(
			'identity' => array(
				'port' => $port,
				'userid' => $userid,
				'token' => $token
			)
		);
		
		if(is_array($feedback_array))
		{
			$result_array['data'] = $feedback_array;
		}else{
			$result_array['data'] = array('result' => strval($feedback_array));
		}
		
		return Zend_Json::encode($result_array);
	}
}